﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;


namespace YouHP.Pages.Ajax
{
    /// <summary>
    /// Ajax相关功能操作类
    /// </summary>
    public class AjaxPage : YouHP.Pages.BasePage
    {
        public AjaxPage()
        {
            string type = RequestTools.GetString("t");
            switch (type)
            {
                case "relatekw":
                    //GetRelateKeyword();
                    break;
            }
        }

        /// <summary>
        /// 获取关键字分词
        /// </summary>
        private void GetRelateKeyword()
        {
            string title = Utils.UrlEncode(Utils.RemoveHtml(Regex.Replace(RequestTools.GetString("titleenc").Trim(), @"\[[^\]]*?\]", string.Empty, RegexOptions.IgnoreCase)));
            string content = Utils.RemoveHtml(Regex.Replace(RequestTools.GetString("contentenc").Trim(), @"\[[^\]]*?\]", string.Empty, RegexOptions.IgnoreCase));
            content = content.Replace(" ", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("　", string.Empty);
            content = Utils.GetUnicodeSubString(content, 500, string.Empty);
            content = Utils.UrlEncode(content);

            title += content;
            //string xmlContent = Utils.GetSourceTextByUrl(string.Format(" http://search.qihoo.com/sint/related_kw.html?title={0}&content={1}&ics=utf-8&ocs=utf-8", title, content));
            string xmlContent = ""; // ZHI_TODO
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlContent);

            XmlNodeList xnl = xmldoc.GetElementsByTagName("kw");
            StringBuilder builder = new StringBuilder();
            foreach (XmlNode node in xnl)
            {
                builder.AppendFormat("{0} ", node.InnerText);

            }

            StringBuilder xmlBuilder = new StringBuilder(string.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
                                            <root><![CDATA[{0}]]></root>", builder.ToString()));

            ResponseXML(xmlBuilder);
        }


        #region Helper
        /// <summary>
        /// 向页面输出xml内容
        /// </summary>
        /// <param name="xmlnode">xml内容</param>
        private void ResponseXML(System.Text.StringBuilder xmlnode)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/XML";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Cache.SetNoStore();
            System.Web.HttpContext.Current.Response.Write(xmlnode.ToString());
            System.Web.HttpContext.Current.Response.End();
        }


        #endregion

    } //class
} //namespace

